//
//  EditPictureViewController.swift
//  CalligraphyUniversal
//
//  Created by 李雷川 on 2017/7/15.
//  Copyright © 2017年 李雷川. All rights reserved.
//

import UIKit
import FZSFCommonTools
import SnapKit
class EditPictureViewController: UIViewController {

    fileprivate var image:UIImage
    lazy var imageView:UIImageView = {
       let imageView = UIImageView()
        return imageView
    }()
    lazy var cancelButton:UIButton = {
        let button = UIButton()
        button.setImage(UIImage.init(named:imageNamed(imageName: "arrow_left")), for: .normal)
        button.addTarget(self, action: #selector(tapCancel(sender:)), for: .touchUpInside)
        return button
    }()
    
    lazy var retakephotoButton:UIButton = {
        let button = UIButton.custom(title: "重新拍照", titleColor: AppColors.foreground, backgroundColor: AppColors.highlight, radius: 4)
        return button
    }()
    
    
    lazy var resumbmitButton:UIButton = {
        let button =  UIButton.custom(title: "重新提交", titleColor: AppColors.foreground, backgroundColor: AppColors.highlight, radius: 4)
        return button
    }()
    
    lazy var infoLabel:UILabel = {
        let  label = UILabel.createLabel(textColor: AppColors.highlight, font: AppFonts.normal, defaultText: "")
        return label
    }()

    var percent:Float = 0.5
    var startEvaluateClosure:((_ image:UIImage,_ viewController:UIViewController,_ faildHandle:((_ code:Int)->Void)?)->())?
    var hud:FZProgressHUD?
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        startEvaluate()
    }
    
    init(percent aPercent:Float,image aImage:UIImage){
        self.percent = aPercent
        self.image = aImage
        super.init(nibName: nil, bundle: nil)
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return .lightContent
    }
    
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.setNavigationBarHidden(true, animated: true)
    }

    func initUI(){
        view.backgroundColor = UIColor.black
        imageView.image = image
        view.addSubview(imageView)
        view.addSubview(cancelButton)
        view.addSubview(retakephotoButton)
        view.addSubview(resumbmitButton)
        view.addSubview(infoLabel)
        retakephotoButton.isHidden = true
        resumbmitButton.isHidden = true
        infoLabel.isHidden = true
        
      
        
 
        retakephotoButton.addTarget(self, action: #selector(tapRetakephoto(sender:)), for: .touchUpInside)
        

        resumbmitButton.addTarget(self, action: #selector(tapConfirm(sender:)), for: .touchUpInside)
        

        cancelButton.snp.makeConstraints { (make) in
            make.leading.equalTo(4)
            make.top.equalTo(15 + topMargin)
        }
        imageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.centerY.equalTo(self.view).offset(-30)
            make.width.height.equalTo(self.view.width>self.view.height ? view.snp.height:view.snp.width).multipliedBy(percent)
        }
        
        infoLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.top.equalTo(imageView.snp.bottom).offset(16)
        }

        retakephotoButton.snp.makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.bottom.equalTo(-52)
            make.size.equalTo(CGSize.init(width: 335, height: 42))
        }
        resumbmitButton.snp.makeConstraints { (make) in
            make.edges.equalTo(retakephotoButton)
        }

    }
    
    
    
    func setupDrawView(){
    }
    
    @objc func tapCancel(sender:UIButton){
        self.navigationController?.popViewController(animated: false)
        
    }
    @objc func tapRetakephoto(sender:UIButton){
        self.navigationController?.popViewController(animated: false)
        
    }
    
    
    @objc func tapConfirm(sender:UIButton){
        resumbmitButton.isHidden = true
        infoLabel.isHidden = true
        startEvaluate()
    }
    
    func startEvaluate(){
        guard networkIsReachiable() else{
            self.retakephotoButton.isHidden = true
            self.resumbmitButton.isHidden = false
            self.infoLabel.isHidden = false
            self.infoLabel.text = errorMessage.timeout
            return
        }
        self.infoLabel.isHidden = true
        self.startEvaluateClosure?(image,self,{[weak self](code)in
            if(code == -4){
                self?.infoLabel.isHidden = false
                self?.retakephotoButton.isHidden = false
                self?.resumbmitButton.isHidden = true
                self?.infoLabel.text = "请提交正确作品"
            }
            else{
                self?.infoLabel.isHidden = false
                self?.retakephotoButton.isHidden = true
                self?.resumbmitButton.isHidden = false
                self?.infoLabel.text = "测评失败，请稍后重试"
            }
        })
    }
    
    
    
    
}
